import { createReducer, on, Action } from '@ngrx/store';
import { PlayState } from '../entity';
import { setPlaying, setPlayMode, setPlayList, setSongList, setCurrentIndex } from '../actions/play.actions';

export const initPlayState: PlayState = {
  playing: false,
  playMode: { type: 'loop', label: '循环' },
  playList: [],
  songList: [],
  currentIndex: -1
};

const reducers = createReducer(
  initPlayState,
  on(setPlaying, (state, { playing }) => ({ ...state, playing })),
  on(setPlayMode, (state, { playMode }) => ({ ...state, playMode })),
  on(setPlayList, (state, { playList }) => ({ ...state, playList })),
  on(setSongList, (state, { songList }) => ({ ...state, songList })),
  on(setCurrentIndex, (state, { currentIndex }) => ({ ...state, currentIndex }))
);

export function playerReducer(state: PlayState, action: Action) {
  return reducers(state, action);
}
